<?php
class AccountController extends CustomControllerAction {
	public function init() {
		parent::init();
		$this->breadcrumbs->addStep('用户中心', $this->getUrl('index', 'account'));
	}
	public function indexAction() {

	   $auth = Zend_Auth::getInstance();
       if (!$auth->hasIdentity()) {
            $this->_redirect('/');
       }
       $identity = $auth->getStorage()->read();
       
       $this->view->user = $identity;
	}
	public function fetchpasswordAction() {
	    if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect('/account');
        }
		$errors = array();
		$action = $this->getRequest()->getQuery('action');
		
		if ($this->getRequest()->isPost()) {
			$action = 'submit';
		}

		switch ($action) {
			case 'submit' : 
				$username = trim($this->getRequest()->getPost('username'));
				if (strlen($username) == 0) {
					$errors['username'] = '请输入用户名';
				} else {
					$user = new DatabaseObject_User($this->db);
					if ($user->load($username, 'username')) {
						$user->fetchPassword();
						$url = '/account/fetchpassword?action=complete';
						$this->_redirect($url);
					} else {
						$errors['username'] = '用户名输入错误';
					}		
				}
				break;
					
			case 'complete' : 
				$this->_redirect('/account/login');	
			
			case 'comfirm'  :
				$id = $this->getRequest()->getQuery('id');
				$key = $this->getRequest()->getQuery('key');
				$user = new DatabaseObject_User($this->db);
				if(!$user->load($id)) {
					$errors['confirm'] = '验证失败';
				} elseif (!$user->confirmNewPassword($key)) {
					$errors['confirm'] = '验证失败';
				}
				break;
		}
		
		$this->breadcrumbs->addStep('获取密码', 
		      $this->getUrl('fetchpassword'));
		
		$this->view->errors = $errors;
		$this->view->action = $action;
	}
	public function registerAction(){
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$this->_redirect('/account');
		}
		
		$request = $this->getRequest();
		$fp = new FormProcessor_UserRegistration($this->db);
		if ($request->isPost()) {
			if ($fp->process($request)) {
				$this->messager->addMessage('注册完成，请登录');
				$this->_redirect($this->getUrl('index'));
				
			}
		}
		
		$this->breadcrumbs->addStep('创建账户', $this->getUrl('register'));
        
		$this->view->fp = $fp;
	}
	
	public function registercompleteAction() {
		$session = new Zend_Session_Namespace('registration');
		$user = new DatabaseObject_User($this->db);
		
		if (!$user->load($session->userid)) {
			$this->_forward('register');
			return;
		}
		
		$this->breadcrumbs->addStep('创建账户', 
		  $this->getUrl('register'));
		$this->breadcrumbs->addStep('创建完成', 
		      $this->getUrl('registercomplete'));
		
		$this->view->username = $user->username;
	}
	
	public function loginAction() {
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$this->_redirect('/account');
		}
		$request = $this->getRequest();
		$redirect = $request->getPost('redirect');
		
		if (strlen($redirect) == 0) {
			$redirect = $request->getServer('REQUEST_URI');
		} 
		if (strlen($redirect) == 0) {
			$redirect = '/account';
		}
		
		$errors = array();
		
		if ($request->isPost()) {
			$username = $request->getPost('username');
			$password = $request->getPost('password');
			
			if (strlen($username) == 0) {
				$errors['username'] = '请填入用户名';
			}
			if (strlen($password) == 0) {
				$errors['password'] = '请填入密码';
			}
			
			if (count($errors) == 0) {
				$adapter = new Zend_Auth_Adapter_DbTable($this->db,
														 'users',
														 'username',
														 'password',
														 'md5(?)');
				
				$adapter->setIdentity($username);
				$adapter->setCredential($password);
				
				$result = $auth->authenticate($adapter);
				
				if ($result->getCode() == Zend_Auth_Result::SUCCESS) {
					$user = new DatabaseObject_User($this->db);
					$user->load($adapter->getResultRowObject()->userid);
					$user->loginSuccess();
				
					$identity = $user->createAuthIdentity();
					$auth->getStorage()->write($identity);
					$this->_redirect($redirect);
				} else {
					$errors['password'] = DatabaseObject_User::LoginFailure($username
						, $result->getCode());
				}
			}
		}
		
		$this->breadcrumbs->addStep('用户登陆', 
		      $this->getUrl('login'));
		
		$this->view->errors = $errors;
		$this->view->redirect = $redirect;
	}
	
	public function detailAction() {
	    $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/');
        }
        $identity = $auth->getIdentity();
        
        $request = $this->getRequest();
        $fp = new FormProcessor_UserDetail($this->db, $identity->userid);
        
        if($request->isPost()) {
        	if($fp->process($request)) {
        		$this->_redirect($this->getUrl('detailcomplete'));
        	}
        }
        
        $this->breadcrumbs->addStep('编辑信息', $this->getUrl('detail'));
        $this->view->fp = $fp;
	}
	
	public function detailcompleteAction() {
	    $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/');
        }
        $identity = $auth->getStorage()->read();
        $user = new DatabaseObject_User($this->db);
        
        if (!$user->load($identity->userid)) {
            $this->_redirect($this->getUrl('index'));
            return;
        }
        
        $identity = $user->createAuthIdentity();
        $auth->getStorage()->write($identity);
        
        $this->breadcrumbs->addStep('编辑信息', 
              $this->getUrl('detail'));
        $this->breadcrumbs->addStep('编辑完成', 
              $this->getUrl('detailcomplete'));
	}
	
	public function logoutAction() {

		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect('/');
	}
}
?>